package com.tanhua.controller;

import com.tanhua.exception.BusinessException;
import com.tanhua.mondb.Movement;
import com.tanhua.server.MovementsService;
import com.tanhua.vo.MovementsVo;
import com.tanhua.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/movements")
public class MovementController {
    @Autowired
    private MovementsService movementsService;
    /*
    发布动态
    * */
    @PostMapping
    public ResponseEntity movements(Movement movement,
                                    MultipartFile imageContent[]) throws IOException, BusinessException {

        movementsService.publishMovements(movement,imageContent);
       return ResponseEntity.ok(null);
    }
    /*查看个人动态
    * */
    @GetMapping("/all")
    public ResponseEntity movementsByUserId(Long userId,
                                            @RequestParam(defaultValue="1") Integer page,
                                            @RequestParam(defaultValue="10") Integer pageSize){
       PageResult ps =  movementsService.movementsByUserId(userId,page ,pageSize);
        return  ResponseEntity.ok(ps);

        }
     /*
     * 查询好友的动态
     * */
     @GetMapping
     public ResponseEntity movements(@RequestParam(defaultValue="1") Integer page,
                                     @RequestParam(defaultValue="10") Integer pageSize){
         PageResult pr = movementsService.findFriendMovements(page,pageSize);
        return  ResponseEntity.ok(pr);
     }
     /*
     * 查询推荐好友
     * */
     @GetMapping("/recommend")
     public ResponseEntity recommend(@RequestParam(defaultValue="1") Integer page,
                                     @RequestParam(defaultValue="10") Integer pageSize){
        PageResult pr =  movementsService.findRecommendMovements(page,pageSize);
         return ResponseEntity.ok(pr);
     }
     /*
     * 查询单条动态
     * */
     @GetMapping("/{id}")
     public ResponseEntity findById(@PathVariable("id") String movementId){
         MovementsVo vo = movementsService.findMovementById(movementId);
         return ResponseEntity.ok(vo);
     }
     /*
     * 查询点赞数
     * */
     @GetMapping("/{id}/like")
     public ResponseEntity like(@PathVariable("id") String movementId) throws BusinessException {
         Integer likeCount = movementsService.likeComment(movementId);
         return ResponseEntity.ok(likeCount);
     }
    /**
     * 取消点赞
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String movementId) throws BusinessException {
        Integer likeCount = movementsService.dislikeComment(movementId);
        return ResponseEntity.ok(likeCount);
    }
    /*
    * 喜欢
    * */
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String movementId) throws BusinessException {
        Integer likeCount = movementsService.loveComment(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消喜欢
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable("id") String movementId) throws BusinessException {
        Integer likeCount = movementsService.disloveComment(movementId);
        return ResponseEntity.ok(likeCount);
    }
}
